<!-- 资源位编辑弹窗 -->
<template>
  <el-dialog
    title="修改协议"
    :visible="visible"
    width="1050px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
    custom-class="import-dialog"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="协议标题:"
        prop="title">
        <el-input
          :maxlength="255"
          v-model="form.title"
          placeholder="请输入协议标题"
          clearable/>
      </el-form-item>



      <el-form-item label="内容:" prop="content">
        <tinymce-editor v-model="form.content" :init="initEditor"/>

      </el-form-item>


    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>



<script>
// import axios from "axios";
import TinymceEditor from "@/components/TinymceEditor";
export default {
  name: 'BoxAgreementEdit',
  components: {TinymceEditor},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  // components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        title: [
          {required: true, message: '请输入协议标题', trigger: 'blur'}
        ],

        content: [
          {required: true, message: '请输入协议内容', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      bannerOptions: [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },

  computed: {
    filteredDeviceArr() {
      // 在这个计算属性中筛选设备数组，返回一个新的对象
      return Object.keys(this.deviceArr).reduce((filtered, key) => {
        if (key !== '1') {
          filtered[key] = this.deviceArr[key];
        }
        return filtered;
      }, {});
    },
    // 初始化富文本
    initEditor() {
      return {
        height: 600,
        branding: false,
        skin_url: '/tinymce/skins/ui/oxide',
        content_css: '/tinymce/skins/content/default/content.css',
        language_url: '/tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
        toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
        toolbar_drawer: 'sliding',
        images_upload_handler: (blobInfo, success, error) => {
          let file = blobInfo.blob();
          // 使用axios上传
          const formData = new FormData();
          formData.append('file', file, file.name);
          this.$http.post('/upload/uploadImageOss', formData).then(res => {
            if (res.data.code == 0) {
              success(res.data.data);
            } else {
              error(res.data.msg);
            }
          }).catch(e => {
            console.error(e);
            error(e.message);
          });
        },
        file_picker_types: 'media',
        file_picker_callback: () => {
        }
      }
    },
  },
  created() {
    this.initBannerData()
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/boxAgreement/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    confirmClose() {
      this.$confirm('确定要返回吗？', '温馨提示').then(() => {
        this.updateVisible(false)
        if (!this.isUpdate) {
          this.form = {};
        }
      }).catch(() => {

      });
    },
  }
}
</script>

